﻿using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Verse;
using Verse.AI;

namespace SZ.Mech
{
    public class CompProperties_JumpExplosion : CompProperties
    {
        public int CD;
        public CompProperties_JumpExplosion()
        {
            this.compClass = typeof(JumpComp);
        }

    }
    public class JumpComp : ThingComp
    {
        public Pawn pawn
        {
            get
            {
                return parent as Pawn;
            }
        }
        public Verb_JumpExplosion jump;
        public int cdTick;
        public CompProperties_JumpExplosion Props => (CompProperties_JumpExplosion)props;
        public override void PostSpawnSetup(bool respawningAfterLoad)
        {
            base.PostSpawnSetup(respawningAfterLoad);
            foreach (var jumpVerb in pawn.verbTracker.AllVerbs)
            {
                if (jumpVerb is Verb_JumpExplosion)
                {
                    jump = jumpVerb as Verb_JumpExplosion;
                }
            }
        }

        public override IEnumerable<Gizmo> CompGetGizmosExtra()
        {
            if (pawn.Drafted)
            {
                Command_VerbTarget command_Target = new Command_VerbTarget();
                command_Target.verb = jump;
                command_Target.icon = ContentFinder<Texture2D>.Get("UI/Abilities/破军跳跃技能_画板 1");

                if (cdTick <= 0)
                {
                    command_Target.Disabled = false;
                    command_Target.defaultLabel = "跳跃";
                }
                else
                {
                    command_Target.Disabled = true;
                    command_Target.defaultLabel = $"跳跃冷却{cdTick.ToStringSecondsFromTicks()}";
                }
                yield return command_Target;
            }
            else
            {
                yield break;
            }
        }

        public override void CompTick()
        {
            base.CompTick();
            if(pawn==null || pawn.CurJob==null)
            {
                return;
            }

            //if (pawn.CurJob.def == JobDefOf.CastJump)
            //{
            //    cdTick = Props.CD;
            //}
            //else
            //{
                cdTick--;
            //}

        }

        public override void CompTickLong()
        {
            base.CompTickLong();
            Pawn p = (parent as Pawn);

            if (p.Downed || p.Dead)
            {
                return;
            }
            if (!p.IsColonyMechPlayerControlled && cdTick <= 0)
            {
                if (p.jobs.curDriver is JobDriver_AttackMelee)
                {
                    //var melee = (pawn.jobs.curDriver as JobDriver_AttackMelee);
                    LocalTargetInfo target = p.CurJob.targetA;
                    if (target != null)
                    {
                        Jump(pawn, target);
                        cdTick = Props.CD;
                    }
                }
                else if (pawn.mindState.meleeThreat != null)
                {
                    Jump(pawn, pawn.mindState.meleeThreat);
                    cdTick = Props.CD;
                }
            }

        }
        public void Jump(Pawn pawn, LocalTargetInfo targetPosition)
        {
            float distance = pawn.Position.DistanceTo(targetPosition.Cell);
            if (distance < 30 && distance > 3)
            {
                JumpExplosionUtility.DoJump(pawn, targetPosition, jump.ReloadableCompSource, jump.verbProps);
            }
        }
        public override void PostExposeData()
        {
            Scribe_Values.Look(ref cdTick, "cdTick", 0);
            base.PostExposeData();
        }
    }
}
